package com.feicheng.house.service.impl;

import com.feicheng.house.entity.BuyOrder;
import com.feicheng.house.entity.Menjin;
import com.feicheng.house.entity.Permission;
import com.feicheng.house.entity.Shop;
import com.feicheng.house.entity.ShopMenjin;
import com.feicheng.house.entity.User;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.mapper.ShopMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IBuyOrderService;
import com.feicheng.house.service.IMenjinService;
import com.feicheng.house.service.IShopMenjinService;
import com.feicheng.house.service.IShopService;
import com.feicheng.house.service.IUserService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-05
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
	/**
	 * @see IShopService.getAllShop
	 */
	@Override
	public ResultInfo getAllShop(Integer currentPage, Integer pageSize, String searchKey) {
		//构建条件状态
		QueryWrapper<Shop> wrapper = new QueryWrapper<Shop>();
		//根据searchKey是否为空进行关键字查询
		if(StringUtils.hasLength(searchKey)) {
			wrapper.and(i->i.like("shop_name",searchKey)
					.or().like("shop_address",searchKey)
					.or().like("shop_phone",searchKey));
		}
		wrapper.eq("delete_flag", true);
		IPage<Shop> page = new Page<Shop>(currentPage,pageSize);
		return ResultInfo.success(this.page(page,wrapper));
	}
	@Autowired
	IMenjinService iMenjinService;
	
	@Autowired
	IShopMenjinService iShopMenjinService;
	
	@Autowired
	IBuyOrderService iBuyOrderService;
	
	@Autowired
	IUserService iUserService;
	
	/**
	 * @see IShopService.one
	 */

	@Override
	public ResultInfo getOne(Integer shopId) {
		//根据门店id查询门店详情
		Shop shop = this.getById(shopId);
		
		//构建JSONObject对象，将门店对象和所有的门禁对象扔进去
		JSONObject result = new JSONObject();
		result.put("shop", shop);
		result.put("allMenjin", iMenjinService.list());
		List<Integer> list1 = new ArrayList<Integer>();
		result.put("allUser", iUserService.list());
		List<Integer> list3 = new ArrayList<Integer>();
		
		//根据门店id查询门店门禁中间表对应表关系
		QueryWrapper<ShopMenjin> wrapper = new QueryWrapper<ShopMenjin>();
		wrapper.eq("shop_id", shopId);
		List<ShopMenjin> list2 = iShopMenjinService.list(wrapper);
	    
		//判断如果门店门禁中间表有对应关联再根据对应关系去查门禁表查询出来对应的门禁
		if(null != list2 && !list2.isEmpty()) {
			for(ShopMenjin a : list2) {
				list1.add(a.getMenjinId());
			}
			result.put("menjin", iMenjinService.listByIds(list1));
		}
//		//根据门店id查询门店用户表中间表对应表关系
//		QueryWrapper<ShopUser> wrapper1 = new QueryWrapper<ShopUser>();
//		wrapper.eq("shop_id", shopId);
//		List<ShopUser> list4 = iShopUserService.list(wrapper1);
//			    
//		//判断如果门店用户中间表有对应关联再根据对应关系去查用户表查询出来对应的用户
//		if(null != list4 && !list4.isEmpty()) {
//			for(ShopUser a : list4) {
//				list3.add(a.getUserId());
//			}
//			result.put("user", iUserService.listByIds(list3));
//		}
		return ResultInfo.success(result);
	    
	}

	@Override
	public ResultInfo deleteshop(Integer ShopId) {
		Shop shop = this.getById(ShopId);
		shop.setDeleteFlag(false);
		//删除原来的门店门禁中间表关系
		QueryWrapper<ShopMenjin> wrapper = new QueryWrapper<ShopMenjin>();
		wrapper.eq("shop_id", shop.getShopId());
		iShopMenjinService.remove(wrapper);
		if(!this.updateById(shop)) {
			throw new SystemException("删除失败");
		}
		return ResultInfo.success(this.updateById(shop));
	}

	@Override
	public ResultInfo add(Shop shop, String menjins) {
		
		//将门店对象新增到门店中
		this.save(shop);
		//如果menjins有长度，在构建门店门禁中间表对象，将多个门店门禁中间表对象新增到门店门禁中间表中
		if(StringUtils.hasLength(menjins)) {
			
			JSONArray array = JSONArray.parseArray(menjins);
			for(int i = 0;i < array.size();i++) {
				ShopMenjin shopMenjin = new ShopMenjin();
				shopMenjin.setShopId(shop.getShopId());
				shopMenjin.setMenjinId((Integer)array.get(i));
				iShopMenjinService.save(shopMenjin);
			}
		}
		return ResultInfo.success();
	}

	@Override
	public ResultInfo change(Shop shop,String menjins) {

		//修改根据shopId修改shop对象
		this.updateById(shop);
		
		//如果menjins有长度那么就去删除原来的门店门禁中间表关系然后在添加门店门禁表关系
		if(StringUtils.hasLength(menjins)) {
			QueryWrapper<ShopMenjin> wrapper = new QueryWrapper<ShopMenjin>();
			wrapper.eq("shop_id", shop.getShopId());
			iShopMenjinService.remove(wrapper);
			
			JSONArray array = JSONArray.parseArray(menjins);
			
		//构建门店门禁中间表对象
			List<ShopMenjin> smlist = new ArrayList<ShopMenjin>();
			for(int i = 0;i < array.size();i++) {
				ShopMenjin shopMenjin = new ShopMenjin();
				shopMenjin.setShopId(shop.getShopId());
				shopMenjin.setMenjinId((Integer)array.get(i));
				smlist.add(shopMenjin);
			}
		//新增进去
			iShopMenjinService.saveBatch(smlist);
			
			
		}
		return ResultInfo.success(shop);
	}

	@Override
	public ResultInfo all() {
		//构建JSONObject对象，将门店对象和所有的门店人数扔进去
		JSONObject result = new JSONObject();
		result.put("allShop",this.list());
		List<Integer> list = new ArrayList<Integer>();
		for(Shop a : this.list()) {
			QueryWrapper<BuyOrder> wrapper = new QueryWrapper<BuyOrder>();
			wrapper.eq("shop_id", a.getShopId()).isNull("exercise_end").last("and DATEDIFF(exercise_start, NOW()) = 0");
			list.add(iBuyOrderService.count(wrapper));
		}
		result.put("count",this.list());
		return ResultInfo.success(result);
	}

}
